正則表達式 (RegExp)是一種專用的形式語法,用於描述、匹配和操作字串資料中的模式。在 JavaScript 中,RegExp 可作為引擎執行搜尋與驗證操作的藍圖。
1. 定義語法
定義模式主要有兩種方式: 字面量表示法 (var re2 = /abc/;),在腳本載入時編譯,以及 RegExp 建構函數 (var re1 = new RegExp("abc");),可根據變數動態建立模式。
2. .test() 方法
該 .test() 方法是應用此語法最基本的方式;它會回傳一個布林值,指出模式是否在目標字串中的任何位置存在。某些字元如 + 具有特殊功能意義,必須以反斜線 轉義 來表示字面意義(例如, /eighteen\+/) 才能被當作字面字元進行匹配。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
Which notation is compiled when the script is first loaded?
The RegExp constructor notation.
The literal value notation (e.g., /abc/).
The string.match() method.
The JSON.parse() notation.
✅ Correct!
Literal notation is static and compiled once when the code is evaluated.❌ Incorrect
The RegExp constructor is evaluated at runtime, making it slower but more flexible for dynamic strings.QUESTION 2
What is the return type of the RegExp .test() method?
String
Number
Boolean
Object
✅ Correct!
Correct! It returns true if the pattern is found and false otherwise.❌ Incorrect
Think about it: does the pattern exist or not? That's a binary choice.QUESTION 3
How do you match a literal plus sign (+) in a regular expression?
By using double quotes.
By escaping it with a backslash: \+.
By placing it in parentheses.
Plus signs are always matched literally by default.
✅ Correct!
Because '+' is a special quantifier (one or more), it must be escaped to be treated as text.❌ Incorrect
Special characters in RegExp grammar require a backslash to be interpreted literally.QUESTION 4
Which flag is used to perform a case-insensitive search?
The 'g' flag.
The 's' flag.
The 'i' flag.
The 'c' flag.
✅ Correct!
The 'i' flag stands for 'insensitive'.❌ Incorrect
'g' is for global matching. Case-insensitivity uses 'i'.QUESTION 5
When would you prefer the RegExp constructor over literal notation?
When the pattern is constant throughout the program.
When you need to build a pattern from a variable string.
When you want better performance.
Literal notation is always preferred.
✅ Correct!
The constructor accepts strings, allowing you to create patterns dynamically based on user input or other variables.❌ Incorrect
Literals are fixed. If your pattern depends on a variable, you must use the constructor.Case Study: Product Code Validation
Applying RegExp to Database Filtering
A logistics company uses product codes that start with the prefix 'abc'. However, legacy systems sometimes enter these in uppercase ('ABC') or mixed case ('aBc'). You need to create a validator that identifies these codes regardless of case.
Q
1. Write the literal regular expression to match 'abc' case-insensitively.
Solution:
The pattern is
The pattern is
/abc/i. The 'i' flag ensures that 'ABC', 'abc', and 'AbC' all return true when tested.Q
2. If you were receiving the prefix as a user-provided string variable named 'prefix', how would you initialize the RegExp?
Solution:
You would use the constructor:
You would use the constructor:
new RegExp(prefix, "i"). This allows the pattern to be dynamic while still applying the case-insensitive flag.Q
3. Why does
/abc/.test("abcde") return true even though the strings aren't identical?Solution:
By default, a regular expression looks for the pattern anywhere within the string, not just an exact full-string match. Since 'abc' exists at the start of 'abcde', the test passes.
By default, a regular expression looks for the pattern anywhere within the string, not just an exact full-string match. Since 'abc' exists at the start of 'abcde', the test passes.